Namespace persistent.Base.Customers

    Public Class Customer
        Implements ICustomerRepresenter

#Region "Instance Members"
        Private _Source As DataRow
        Private _Customers As ArrayList
        Private _Contracts As ArrayList
        Private _CustomerName As String = String.Empty
        Private _CustomerStructure As CustomerStructures
        Private _ParentCustomer As Customer
        'Private _CustomersTable As DataTable

        Private Sub New(ByVal custcode As String)
            Me._Source = persistent.myDBConn.myORATable(String.Format("select * from customer_all where custcode = '{0}'", custcode)).Rows(0)
            Me._CustomerName = persistent.common.GetNameOfCompany(custcode).Rows(0)(0)
            Me._CustomerStructure = Asc(persistent.myDBConn.ORAScalar(String.Format("select tmtype from tarifftype where tmcode = {0}", Me._Source("TMCODE"))))

            If IsDBNull(Me._Source("CUSTOMER_ID_HIGH")) OrElse Not Customer.CheckExistenceByCustomerID(Me._Source("CUSTOMER_ID_HIGH")) Then
                Me._ParentCustomer = Nothing
            Else
                Me._ParentCustomer = Customer.GetByCustomerID(Me._Source("CUSTOMER_ID_HIGH"))
            End If

        End Sub

        Public ReadOnly Property CustCode() As String
            Get
                Return Me._Source("CUSTCODE")
            End Get
        End Property

        Public ReadOnly Property CustomerID() As Integer Implements ICustomerRepresenter.ID
            Get
                Return Me._Source("CUSTOMER_ID")
            End Get
        End Property

        Public ReadOnly Property CustomerName() As String
            Get
                Return Me._CustomerName
            End Get
        End Property

        Public ReadOnly Property CustomerStructure() As CustomerStructures Implements ICustomerRepresenter.CustomerStructure
            Get
                Return Me._CustomerStructure
            End Get
        End Property

        Public ReadOnly Property Contracts() As Contract()
            Get
                If Me._Contracts Is Nothing Then
                    Me._Contracts = New ArrayList

                    For Each coID As Integer In CustomerContractInteractions.ConvertCustomerIDtoContractID(CustomerContractInteractions.ConvertCustcodeToCustomerID(CustCode))
                        Me._Contracts.Add(Contract.GetByContractID(coID, Me))
                    Next

                End If

                Dim __Contracts(Me._Contracts.Count - 1) As Contract

                For contractIndex As Integer = 0 To Me._Contracts.Count - 1
                    __Contracts(contractIndex) = Me._Contracts(contractIndex)
                Next

                Return __Contracts
            End Get
        End Property

        Public Function ToDataRow() As DataRow
            Return Me._Source
        End Function

#End Region

#Region "Static Members"

#Region "Existence Checks (static)"

        Public Shared Function CheckExistenceByCustomerID(ByVal customerID As Integer) As Boolean
            Return persistent.Base.Customers.CustomerContractInteractions.ConvertCustomerIDtoCustcode(customerID) <> String.Empty
        End Function

        Public Shared Function CheckExistenceByCustcode(ByVal custCode As String) As Boolean
            Return persistent.Base.Customers.CustomerContractInteractions.ConvertCustcodeToCustomerID(custCode) > -1
        End Function

#End Region

#Region "Customer Retrival (static)"

        Public Shared Function GetByCustomerID(ByVal customerID As Integer) As Customer
            If Not CheckExistenceByCustomerID(customerID) Then
                Throw New Exception("No customer found by CustomerID: " & customerID)
            End If

            Return GetByCustcode(persistent.Base.Customers.CustomerContractInteractions.ConvertCustomerIDtoCustcode(customerID))
        End Function

        Public Shared Function GetByCustcode(ByVal custCode As String) As Customer
            If Not CheckExistenceByCustcode(custCode) Then
                Throw New Exception("No customer found by CustCode: " & custCode)
            End If

            Return New Customer(custCode)
        End Function

#End Region

#End Region

        Public ReadOnly Property Children() As ICustomerRepresenter() Implements ICustomerRepresenter.Children
            Get
                If Me._Customers Is Nothing Then
                    Me._Customers = New ArrayList

                    For Each customerRow As DataRow In persistent.myDBConn.myORATable(String.Format("select customer_id from customer_all where customer_id_high = {0}", Me._Source("CUSTOMER_ID"))).Rows
                        Me._Customers.Add(Customer.GetByCustomerID(customerRow("CUSTOMER_ID")))
                    Next

                End If
                Return Me._Customers.ToArray()
            End Get
        End Property

        Public ReadOnly Property IsActive() As Boolean Implements ICustomerRepresenter.IsActive
            Get
                Return Me._Source("CSTYPE").ToString().ToUpper() = "A"
            End Get
        End Property

        Public ReadOnly Property Parent() As ICustomerRepresenter Implements ICustomerRepresenter.Parent
            Get
                Return Me._ParentCustomer
            End Get
        End Property
    End Class

End Namespace